<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of WeatherLogic
 *
 * @author Administrator
 */
class WeatherLogic extends BaseLogic{
    //put your code here
    public function __construct() {
        parent::__construct();
        //四个月备份一次，重新新建一张表
        $this->table=  $this->load("weather");
        $premoth=date("Y_m",strtotime("-4 month",  time()));
        $checkdb="SELECT count(*) as isexist FROM information_schema.TABLES WHERE table_name = 'jp_weather_".$premoth."' AND TABLE_SCHEMA = '".MysqlDB::get_db_name()."'";
        $res=$this->table->execute($checkdb);
        if($res[0]["isexist"]==0){
            $this->table->query("RENAME table jp_weather to jp_weather_".$premoth);
            $createsql="CREATE TABLE `jp_weather` (
                `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '唯一标识id',
                `nameen` varchar(50) DEFAULT NULL COMMENT '拼音',
                `cityname` varchar(50) DEFAULT NULL COMMENT '城市名称',
                `city_id` int(11) DEFAULT '0' COMMENT '城市id',
                `weatherID` varchar(10) DEFAULT NULL COMMENT '天气网的城市地区编码',
                `area_id` int(11) DEFAULT '0' COMMENT '地区id',
                `temp` decimal(11,2) DEFAULT '0.00' COMMENT '摄氏实时温度',
                `tempf` decimal(11,2) DEFAULT NULL COMMENT '实时华氏温度',
                `wd` varchar(50) DEFAULT NULL COMMENT '实时风向',
                `wde` varchar(10) DEFAULT NULL COMMENT '实时风向英文缩写',
                `ws` varchar(50) DEFAULT NULL COMMENT '实时风力',
                `wse` varchar(50) DEFAULT NULL COMMENT '实时风速',
                `sd` int(11) DEFAULT NULL COMMENT '实时湿度百分比',
                `rtime` int(11) DEFAULT '0' COMMENT '实时更新的时间',
                `weather` varchar(10) DEFAULT NULL COMMENT '实时天气(晴,阴,雨)等',
                `aqiname` varchar(10) DEFAULT NULL COMMENT '实时空气质量级别',
                `aqi` int(11) DEFAULT '0' COMMENT '实时空气质量',
                `day_temp` int(11) DEFAULT '0' COMMENT '白天温度',
                `day_wea` varchar(50) DEFAULT NULL COMMENT '白天天气情况',
                `day_wd` varchar(50) DEFAULT NULL COMMENT '白天风向',
                `day_ws` varchar(50) DEFAULT NULL COMMENT '白天风力',
                `sun_up` int(11) DEFAULT '0' COMMENT '日出时间',
                `night_temp` int(11) DEFAULT '0' COMMENT '晚上温度',
                `night_wea` varchar(50) DEFAULT NULL COMMENT '夜间天气情况',
                `night_wd` varchar(50) DEFAULT NULL COMMENT '晚上风向',
                `night_ws` varchar(50) DEFAULT NULL COMMENT '晚上风力',
                `sun_down` int(11) DEFAULT '0' COMMENT '日落时间',
                `create_time` int(11) DEFAULT '0' COMMENT '日期',
                `edit_time` datetime DEFAULT NULL COMMENT '更新时间',
                `edit_time2` datetime DEFAULT NULL COMMENT '非实时部分的更新时间',
                PRIMARY KEY (`id`),
                KEY `city_id` (`city_id`) USING BTREE,
                KEY `weatherID` (`weatherID`,`create_time`) USING BTREE
              ) ENGINE=MyISAM AUTO_INCREMENT=86578 DEFAULT CHARSET=utf8";
            $this->table->query($createsql);
        }
       
    }
    /**
     * 判断城市是否存在
     * @param type 城市名称
     * @return boolean 存在返回true 否则返回false
     */
    public function existCity_name($city_name){
        if($city_name){
            $today=strtotime(date("Y-m-d")." 00:00:00");
            $res=$this->table->get_count("cityname='{$city_name}' and create_time={$today}");
            if($res>0){
                return TRUE;
            }
        }
        return FALSE;
    }
    
    /**
     * 判断地区是否存在
     * @param type 地区名称
     * @return boolean 存在返回true 否则返回false
     */
    public function existArea_name($area_name){
        if($area_name){
            $today=strtotime(date("Y-m-d")." 00:00:00");
            $res=$this->table->get_count("cityname='{$area_name}' and create_time={$today}");
            if($res>0){
                return TRUE;
            }
        }
        return FALSE;
    }
    
    /**
     * 判断城市是否存在
     * @param type 城市id
     * @return boolean 存在返回true 否则返回false
     */
    public function existCity_id($city_id){
        if($city_id){
            $today=strtotime(date("Y-m-d")." 00:00:00");
            $res=$this->table->get_count("city_id={$city_id} and create_time={$today}");
            if($res>0){
                return TRUE;
            }
        }
        return FALSE;
    }
    
    /**
     * 判断地区是否存在
     * @param type 地区id
     * @return boolean 存在返回true 否则返回false
     */
    public function existArea_id($area_id){
        if($area_id){
            $today=strtotime(date("Y-m-d")." 00:00:00");
            $res=$this->table->get_count("area_id={$area_id} and create_time={$today}");
            if($res>0){
                return TRUE;
            }
        }
        return FALSE;
    }
    /**
     * 判断weatherid是否存在
     * @param type weatherid
     * @return boolean 存在返回true 否则返回false
     */
    public function existWeather_id($weather_id,$today=0){
        if($weather_id){
            if($today==0){
                $today=strtotime(date("Y-m-d")." 00:00:00");
            }
            $dateinfo=  getdate($today);
            $sysdateinfo=  getdate(time());
            $d1=$dateinfo["year"]+$dateinfo["mon"];
            $d2=$sysdateinfo["year"]+$sysdateinfo["mon"];
            if($d2-$d1>=4){
                if($dateinfo["mon"]<10){
                    $dateinfo["mon"]="0".$dateinfo["mon"];
                }
                $this->table=  $this->load("weather_{$dateinfo["year"]}_{$dateinfo["mon"]}");
            }
            $res=$this->table->get_count("weatherID='{$weather_id}' and create_time={$today}");
            if($res>0){
                return TRUE;
            }
        }
        return FALSE;
    }
    /**
     * 通过城市id获得当天天气信息
     * @param type $city_id 城市id
     * @return boolean 成功返回当天天气情况，否则返回FALSE
     */
    public function getWeatherByCityId($city_id){
        if($city_id){
            $today=strtotime(date("Y-m-d")." 00:00:00");
            $res=$this->table->get_one("*","city_id={$city_id} and create_time={$today}");
            if($res){
                return $res;
            }
        }
        return FALSE;
    }
    /**
     * 获得需要更新的天气信息，非实时部分，一天更新一次的
     * @return boolean  天气信息列表 没有则返回false
     */
    public function getNeedToUpdateWeather(){
        $today=strtotime(date("Y-m-d")." 00:00:00");
        $res=  $this->table->select("*","create_time={$today} and sun_up=0");
        if($res){
                return $res;
        }
        return FALSE;
    }
    /**
     * 通过城市名称和时间获取天气信息
     * @param type $cityname 城市名称或者拼音
     * @param type $date_time 要获取哪个日期的天气
     * @return boolean 天气信息 否则返回false
     */
    public function getWeatherByCityName($cityname,$date_time=0){
        if($date_time==0){
            $date_time=  strtotime(date("Y-m-d")." 00:00:00");
        }
        $dateinfo=  getdate($date_time);
        $sysdateinfo=  getdate(time());
        $d1=$dateinfo["year"]+$dateinfo["mon"];
        $d2=$sysdateinfo["year"]+$sysdateinfo["mon"];
        if($d2-$d1>=4){
            if($dateinfo["mon"]<10){
                $dateinfo["mon"]="0".$dateinfo["mon"];
            }
            $this->table=  $this->load("weather_{$dateinfo["year"]}_{$dateinfo["mon"]}");
        }
        if($cityname){
            $res=$this->table->get_one("*","create_time={$date_time} and (cityname like '%{$cityname}%' or nameen like '%{$cityname}%')");
            if($res){
                return $res;
            }
        }
        return FALSE;
    }
    
    /**
     * 通过天气网城市id和时间获取天气信息
     * @param type $cityname 天气网城市id名称或者拼音
     * @param type $date_time 要获取哪个日期的天气
     * @return boolean 天气信息 否则返回false
     */
    public function getWeatherByWeatherID($weatherID,$date_time=0){
        if($date_time==0){
            $date_time=  strtotime(date("Y-m-d")." 00:00:00");
        }
        $dateinfo=  getdate($date_time);
        $sysdateinfo=  getdate(time());
        $d1=$dateinfo["year"]+$dateinfo["mon"];
        $d2=$sysdateinfo["year"]+$sysdateinfo["mon"];
        if($d2-$d1>=4){
            if($dateinfo["mon"]<10){
                $dateinfo["mon"]="0".$dateinfo["mon"];
            }
            $this->table=  $this->load("weather_{$dateinfo["year"]}_{$dateinfo["mon"]}");
        }
        if($weatherID){
            $res=$this->table->get_one("*","create_time={$date_time} and weatherID='{$weatherID}'");
            if($res){
                return $res;
            }
        }
        return FALSE;
    }
    
}
